##################################################
library(foreign)
library(plyr)
library(reshape)
library(ggplot2)
library(xtable)

################DATASET###########################
rep<-read.dta(file="m100n3-10.dta")

######SUBSETS FOR DIFFERENT NUMBER OF PARTIES#######
combs<-rep
combs3<-subset(combs,combs$n==3)
combs4<-subset(combs,combs$n==4)
combs5<-subset(combs,combs$n==5)
combs6<-subset(combs,combs$n==6)
combs7<-subset(combs,combs$n==7)
combs8<-subset(combs,combs$n==8)
combs9<-subset(combs,combs$n==9)
combs10<-subset(combs,combs$n==10)
##################################################


####FUNCTION TO EXTRACT THE COMBINATION OF PARTY SIZE
####RANK AND PARTY SYSTEM TYPE#####################
result<-function(combs){
	
	##TYPE OF THE PARTY SYSTEM
	combs$type[combs$s1+combs$s2>=51&combs$s1+combs$s3>=51&combs$s2+combs$s3<51]<-"B3"
	combs$type[combs$s1+combs$s4>=51&combs$s2+combs$s3<51]<-"B4"
	combs$type[combs$s1+combs$s5>=51&combs$s2+combs$s3<51]<-"B5"
	combs$type[combs$s1+combs$s6>=51&combs$s2+combs$s3<51]<-"B6"
	combs$type[combs$s1+combs$s7>=51&combs$s2+combs$s3<51]<-"B7"
	combs$type[combs$s1+combs$s8>=51&combs$s2+combs$s3<51]<-"B8"
	combs$type[combs$s1+combs$s9>=51&combs$s2+combs$s3<51]<-"B9"
	combs$type[combs$s1+combs$s10>=51&combs$s2+combs$s3<51]<-"B10"
	combs$type[combs$s1+combs$s2>=51&combs$s1+combs$s3>=51&combs$s2+combs$s3>=51]<-"C"
	combs$type[combs$s1+combs$s2>=51&combs$s1+combs$s3<51&combs$s2+combs$s3<51]<-"D"
	combs$type[combs$s1+combs$s2<51&combs$s1+combs$s3<51&combs$s2+combs$s3<51]<-"E"
	combs$type[combs$s1>=51]<-"A"
	
	###DATA FRAME WITH THE COMBINATIONS OF PARTY SIZE RANK AND PARTY SYSTEM TYPE
	data<-list()
	n<-combs$n[1]
	for (i in 1:combs$n[1]){
			data[[i]]<-as.data.frame(cbind(paste(i,combs$type,sep=""),combs[,i+1],rownames(combs)))
	}
	data<-rbind.fill(data)
	names(data)<-c("ptype","size","id")
	data<-subset(data,data$size!=0)
	data$size<-as.numeric(as.character(data$size))
	
	####RECODING THESE COMBINATIONS TO BROADER CATEGORIES
	data$ptype1<-"Minor"
	data$ptype1[data$ptype%in%c("1C","1D","2C","2D","3C")]<-"Advantaged"
	data$ptype1[data$ptype%in%c("1A")]<-"Majority"
	data$ptype1[data$ptype%in%c("1B3","1B4","1B5","1B6","1B7","1B8","1B9","1B10")]<-"Dominant"
	data$ptype1[data$ptype%in%c("2B3","2B4","2B5","2B6","2B7","2B8","2B9","2B10",
		"3B3","3B4","3B5","3B6","3B7","3B8","3B9","3B10",
		"4B4","4B5","4B6","4B7","4B8","4B9","4B10",
		"5B5","5B6","5B7","5B8","5B9","5B10",
		"6B6","6B7","6B8","6B9","6B10",
		"7B7","7B8","7B9","7B10",
		"8B8","8B9","8B10",
		"9B9","9B10",
		"10B10")]<-"Dominated"
	data$ptype1[data$ptype%in%c("1E","2E","3E","4E","5E","6E","7E","8E","9E","10E")]<-"PEsystem"
	data$ptype<-data$ptype1
	data$ptype1<-NULL
	
	####COMPUTING THE SHARES OF COMBINATIONS FOR DIFFERENT VALUES OF PARTY SIZE
	data$ptype<-as.factor(data$ptype)
	probs<-as.data.frame(matrix(rep(1,length(levels(data$ptype))),nrow=1,ncol=length(levels(data$ptype))))
	for (i in 1:60){
		probs[i,]<-table(data$ptype[data$size==i])/sum(table(data$ptype[data$size==i]))*100
	}
	names(probs)<-levels(data$ptype)
	
	###ADDITIONAL COLUMN FOR TYPE E SYSTEM 
	###WHICH IS OTHERWISE MISSING FOR THE 3 PARTY CASE
	n<-combs$n[1]
	if (n<4){
		probs$PEsystem<-0
	}
	
	probs[,7]<-round(rowSums(probs),1)
	
	return(probs)
}
##################################################

#########APPLYING THE FUNCTION#####################
x3<-result(combs3)
x4<-result(combs4)
x5<-result(combs5)
x6<-result(combs6)
x7<-result(combs7)
x8<-result(combs8)
x9<-result(combs9)
x10<-result(combs10)
data<-list(x3,x4,x5,x6,x7,x8,x9,x10)
##################################################


##################################################
######FUNCTION TO AVERAGE THE SHARES OF COMBINATIONS
#ACROSS THE PARTY SYSTEMS WITH A DIFFERENT NUMBER OF PARTIES
tab<-function(data,size){
	probs<-as.data.frame(matrix(nrow=1,ncol=7))
	for (i in 1:7){
		probs[,i]<-round(mean(c(data[[1]][size,i],data[[2]][size,i],data[[3]][size,i],data[[4]][size,i],
		data[[5]][size,i],data[[6]][size,i],data[[7]][size,i],data[[8]][size,i])),1)
	}
	names(probs)<-names(data[[3]])
	return(probs)
}
##################################################

#######APPLYING THIS FUNCTION#############
resultlist<-list()
for (i in 1:60){
	resultlist[[i]]<-tab(data,i)
}
dat<-as.matrix(rbind.fill(resultlist))
dat<-as.data.frame(dat)
##################################################

########RECODING THE PARTY SIZE RANK AND PARTY SYSTEM
######TYPE COMBINATIONS############################
dat<-dat[,1:6]
dat$Minor<-dat$Minor+dat$Dominated+dat$PEsystem
dat$Dominated<-NULL
dat$PEsystem<-NULL
#dat$Majority<-NULL
##################################################

###########VARIABLE FOR PARTY SIZE##############
dat$Size<-as.numeric(rownames(dat))
##################################################

#############RESHAPING THE DATASET FOR THE GRAPH###
dat<-reshape(dat,direction="long",idvar="Size",varying=list(1:4))
names(dat)[names(dat)=="time"]<-"party"
names(dat)[names(dat)=="Advantaged"]<-"value"
dat$party[dat$party==3]<-"Majority party (P1 in A)"
dat$party[dat$party==4]<-"Other"
dat$party[dat$party==1]<-"P1 or P2 or P3 in top-three system or P1 or P2 in top-two system"
dat$party[dat$party==2]<-"P1 is a strongly dominant party"
##################################################

####REMOVE MAJORITY PARTIES###########
dat<-subset(dat,dat$Size<51)
dat<-subset(dat,dat$party!="Majority party (P1 in A)")
##################################################

###CHANGE THE ORDER OF THE LEVELS OF THE FACTOR VARIABLE
###WITH NAMES OF COMBINATIONS
dat$party<-as.factor(dat$party)
dat$party<-relevel(dat$party,"P1 or P2 or P3 in top-three system or P1 or P2 in top-two system")
dat$party<-relevel(dat$party,"P1 is a strongly dominant party")
##################################################


####CODE FOR FIGURE 2####################
pdf("combinationsgraph.pdf",width=10,height=6)
ggplot(data=dat, aes(x=Size,y=value,group=party)) + 
    geom_line(aes(linetype=party)) + 
    expand_limits(y=0) +     
    scale_linetype_manual(name="Combination of party system type and party size rank",values=c("solid","longdash","dotted")) +
    xlab("Party size") + ylab("Share") + # Set axis labels
    theme_bw() +
    theme(legend.position=c(.265, .5))
dev.off()
##################################################




##################################################
####################################################
#TABLE 2 IN ONLINE APPENDIX B##########################

####FUNCTION TO EXTRACT THE COMBINATION OF PARTY SIZE
####RANK AND PARTY SYSTEM TYPE#####################
resultcomplete<-function(combs){
	
	##TYPE OF THE PARTY SYSTEM
	combs$type[combs$s1+combs$s2>=51&combs$s1+combs$s3>=51&combs$s2+combs$s3<51]<-"B3"
	combs$type[combs$s1+combs$s4>=51&combs$s2+combs$s3<51]<-"B4"
	combs$type[combs$s1+combs$s5>=51&combs$s2+combs$s3<51]<-"B5"
	combs$type[combs$s1+combs$s6>=51&combs$s2+combs$s3<51]<-"B6"
	combs$type[combs$s1+combs$s7>=51&combs$s2+combs$s3<51]<-"B7"
	combs$type[combs$s1+combs$s8>=51&combs$s2+combs$s3<51]<-"B8"
	combs$type[combs$s1+combs$s9>=51&combs$s2+combs$s3<51]<-"B9"
	combs$type[combs$s1+combs$s10>=51&combs$s2+combs$s3<51]<-"B10"
	combs$type[combs$s1+combs$s2>=51&combs$s1+combs$s3>=51&combs$s2+combs$s3>=51]<-"C"
	combs$type[combs$s1+combs$s2>=51&combs$s1+combs$s3<51&combs$s2+combs$s3<51]<-"D"
	combs$type[combs$s1+combs$s2<51&combs$s1+combs$s3<51&combs$s2+combs$s3<51]<-"E"
	combs$type[combs$s1>=51]<-"A"
	
	###DATA FRAME WITH THE COMBINATIONS OF PARTY SIZE RANK AND PARTY SYSTEM TYPE
	n<-combs$n[1]
	data<-list()
	combs$id<-rownames(combs)
	for (i in 1:combs$n[1]){
		combs$rank<-as.character(i)
		combs$ptype<-paste(combs$rank,combs$type,sep="")
		combs$sizetemp<-combs[,i+1]
			data[[i]]<-combs[c("type","ptype","sizetemp","id","rank")]
	}
	data<-rbind.fill(data)
	names(data)<-c("type","ptype","size","id","rank")
	data<-subset(data,data$size!=0)
	data$size<-as.numeric(as.character(data$size))
	data$rank<-as.numeric(as.character(data$rank))
	
	####RECODING THESE COMBINATIONS TO BROADER CATEGORIES
	data$ptype1<-as.character(data$ptype)
	data$ptype1[data$rank>3&data$type=="A"]<-"4A"
	data$ptype1[data$rank>3&data$type=="C"]<-"4C"
	data$ptype1[data$rank>3&data$type=="D"]<-"4D"
	data$ptype1[data$rank>3&data$type=="E"]<-"4E"
	data$ptype1[data$ptype%in%c("1B3","1B4","1B5","1B6","1B7","1B8","1B9","1B10")]<-"1B"
	data$ptype1[data$ptype%in%c("2B3","2B4","2B5","2B6","2B7","2B8","2B9","2B10")]<-"2B"
	data$ptype1[data$ptype%in%c("3B3","3B4","3B5","3B6","3B7","3B8","3B9","3B10")]<-"3B"
	data$ptype1[data$type%in%c("B3","B4","B5","B6","B7","B8","B9","B10")&data$rank>3]<-"4B"
	data$ptype1[data$ptype%in%c("4B4","4B5","4B6","4B7","4B8","4B9","4B10",
		"5B5","5B6","5B7","5B8","5B9","5B10",
		"6B6","6B7","6B8","6B9","6B10",
		"7B7","7B8","7B9","7B10",
		"8B8","8B9","8B10",
		"9B9","9B10",
		"10B10")]<-"4Bk"
	
	data$ptype<-as.factor(data$ptype1)
	data$ptype1<-NULL
	levels(data$ptype)<-c(levels(data$ptype),"1D","1E","2D","2E","3D","3E","4A","4B","4Bk","4C","4D","4E")
	
	####COMPUTING THE SHARES OF COMBINATIONS FOR DIFFERENT VALUES OF PARTY SIZE
	probs<-as.data.frame(matrix(rep(1,length(levels(data$ptype))),nrow=1,ncol=length(levels(data$ptype))))
	for (i in 1:10){
		probs[i,]<-table(data$ptype[data$size>(i*5-5)&data$size<=i*5])/sum(table(data$ptype[data$size>(i*5-5)&data$size<=i*5]))*100
	}
	rownames(probs)<-c("1-5","6-10","11-15","16-20","21-25","26-30","31-35","36-40","41-45","46-50")
	names(probs)<-levels(data$ptype)
	probs<-probs[c("1A","1B","1C","1D","1E","2A","2B","2C","2D","2E","3A","3B","3C","3D","3E","4A","4B","4Bk","4C","4D","4E")]
	
	probs[,22]<-round(rowSums(probs),1)
	
	return(probs)
}
##################################################

######SUBSETS FOR DIFFERENT NUMBER OF PARTIES#######
x3<-resultcomplete(combs3)
x4<-resultcomplete(combs4)
x5<-resultcomplete(combs5)
x6<-resultcomplete(combs6)
x7<-resultcomplete(combs7)
x8<-resultcomplete(combs8)
x9<-resultcomplete(combs9)
x10<-resultcomplete(combs10)
##################################################


dat<-round((x3+x4+x5+x6+x7+x8+x9+x10)/8,1)

xtable(dat,digits=1)






